import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Window;


public abstract class Core {
	private static DisplayMode modes[] = {
		new DisplayMode(800, 600, 32, 0),
		new DisplayMode(800, 600, 24, 0),
		new DisplayMode(800, 600, 16, 0),
		new DisplayMode(640, 480, 32, 0),
		new DisplayMode(640, 480, 24, 0),
		new DisplayMode(640, 480, 16, 0),
	};
	private boolean running;
	protected ScreenManager s;
	
	//stop method
	public void stop(){
		running = false;
	}
	
	//call init and game loop
	public void run(){
		try{
			init();
			gameLoop();
		}finally{
			s.restoreScreen();
		}
	}
	
	//set to full screen
	public void init(){
		s = new ScreenManager();
		DisplayMode dm = s.findFirstCompatibleMode(modes);
		s.setFullScreen(dm);
		
		Window w = s.getFullScreenWindow();
		w.setFont(new Font("Arial", Font.PLAIN, 20));
		w.setBackground(Color.GRAY);
		w.setForeground(Color.WHITE);
		
		running = true;
	}
	
	//main game loop
	public void gameLoop(){
		
		long startTime = System.currentTimeMillis();		//최초시작시간
		long cumTime = startTime;								//누적시간
		
		while(running){
			long timePassed = System.currentTimeMillis() - cumTime;		//누적시간부터 얼마나 지났는지
			cumTime += timePassed;														//누적시간 재계산
			update(timePassed);
			Graphics2D g = s.getGraphics();
			draw(g);																			//이미지를 그린다
			g.dispose();																		//리소스 해제
			s.update();																			
			
			try{
				Thread.sleep(20);
			}catch(Exception e){
				
			}
		}
	}
	
	//update animation
	public void update(long timePassed){
		
	}
	
	//draws to screen
	public abstract void draw(Graphics2D g);
}